package com.firetornado.android.gowallajava;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.drawable.Drawable;


public class ImageOps
{
	public static Drawable ImageOperations(String url, String saveFilename) {
		try {			
			InputStream is = (InputStream) getImage(url);
			Drawable d = Drawable.createFromStream(is, "src");
			is.close();
			return d;
		} catch (MalformedURLException e) {			
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	private static InputStream getImage(String address)
	{
		HttpGet httpRequest = new HttpGet(address);
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = null;
		try {
			response = (HttpResponse) httpclient.execute(httpRequest);
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpEntity entity = response.getEntity();
		BufferedHttpEntity bufHttpEntity = null;
		try {
			bufHttpEntity = new BufferedHttpEntity(entity);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		InputStream instream = null;
		try {
			instream = bufHttpEntity.getContent();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return instream;
    }
	
}